#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    DMainWindow(parent),
    ui(new Ui::MainWindow)
{
    QWidget *tt=new QWidget;
    QHBoxLayout *ttlayout=new QHBoxLayout;
    backButton->setFlat(true);
    backButton->setIconSize(QSize(20,20));
    ttlayout->addWidget(backButton);
    ttlayout->addStretch();
    tt->setLayout(ttlayout);
    tt->hide();
    titlebar()->setCustomWidget(tt);

    ui->setupUi(this);
    ui->stackedWidget->setCurrentIndex(0);

    //主页两个按钮
    connect(ui->pushButton,&QPushButton::clicked,[=](){ui->stackedWidget->setCurrentIndex(1);tt->show();ui->stagemodepage->restart();});

    connect(backButton,&QPushButton::clicked,[=](){ui->stackedWidget->setCurrentIndex(0);tt->hide();ui->stagemodepage->restart();});


    titlebar()->setSeparatorVisible(false);
    titlebar()->setBackgroundTransparent(true);
    //主题切换
    connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, [=](DGuiApplicationHelper::ColorType themeType) {
        if(themeType==DGuiApplicationHelper::DarkType){
            setTheme(true);

        }else {
            setTheme(false);
        }
    });

}
void MainWindow::setTheme(bool isDark)
{
    // 菜单图标
    if(isDark){
        // 黑色模式
        themeIsDark=true;
        QPalette palette = ui->centralWidget->palette();
        palette.setColor(QPalette::Window,QColor("#282828"));
        palette.setColor(QPalette::Base,QColor("#282828"));
        palette.setColor(QPalette::Background,QColor("#282828"));
        ui->centralWidget->setAutoFillBackground(true);
        ui->centralWidget->setPalette(palette);
        backButton->setIcon(QIcon(":/closed-light.svg"));

    }else {
        // 亮色模式
        themeIsDark=false;
        QPalette palette = ui->centralWidget->palette();
        palette.setColor(QPalette::Window,QColor("#FFFFFF"));
        palette.setColor(QPalette::Base,QColor("#FFFFFF"));
        palette.setColor(QPalette::Background,QColor("#FFFFFF"));
        ui->centralWidget->setAutoFillBackground(true);
        ui->centralWidget->setPalette(palette);
        backButton->setIcon(QIcon(":/closed-dark.svg"));

    }

}
void MainWindow::closeEvent(QCloseEvent *e)
{
    QApplication* app;
    app->exit(0);
 }

